// pages/lineList/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    option: null,
    offset: 0,
    limit: 10,
    lLineList: [],
    rLineList: [],
    allLineList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.userInfo = util.isLogin();
    console.log(options);
    this.data.option = options;
    if (options.type == "theme") {
      this.createTheme(options.id)
    } if (options.type == "work")  {
      this.createWork(options.id)
    } else if (options.type == "spots") {
      this.createSpots(options.id)
    }
  },

  goRouteDetail(e) {
    wx.navigateTo({
      url: '/pages/routeDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },

  createTheme (tid) {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/theme',
      data: {
        classify_id: tid,
        offset: that.data.offset,
        limit: that.data.limit,
        user_id: this.data.userInfo ? this.data.userInfo.user_id : ''
      }
    }).then((res) => {
      console.log(res);
      that.showLineList(res);
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  createWork(tid) {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/type',
      data: {
        type: 'workshop',
        offset: that.data.offset,
        limit: that.data.limit,
        user_id: this.data.userInfo ? this.data.userInfo.user_id : ''
      }
    }).then((res) => {
      console.log(res);
      that.showLineList(res);
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  showLineList(res){
    if (!res.rows) {
      return;
    }
    this.data.offset++;//数据正常渲染，才自增
    res.rows.map(item => {
      //手动给每个结果返回is_coll,默认为0，未收藏
      item.line_time_list.begin_time = timeReset(item.line_time_list.begin_time)
      item.line_time_list.end_time = timeReset(item.line_time_list.end_time)
      switch (item.line_time_list.status) {
        case 0:
          item.line_time_list.status = '已取消'
          break;
        case 1:
          item.line_time_list.status = '报名中'
          break;
        case 2:
          item.line_time_list.status = '报名结束'
          break;
        case 3:
          item.line_time_list.status = '已完成'
          break;
      }
    })
    this.data.allLineList = this.data.allLineList.concat(res.rows);
    let lArr = this.data.allLineList.filter((item, index) => index % 2 === 0);
    let rArr = this.data.allLineList.filter((item, index) => index % 2 === 1);
    this.setData({
      lLineList: lArr,
      rLineList: rArr
    });
  },

  createSpots(tid) {
    let that = this;
    util.http({
      url: 'api/miniwechat/index/spots',
      data: {
        scenic_spots_id: tid,
        offset: that.data.offset,
        limit: that.data.limit,
        user_id: this.data.userInfo ? this.data.userInfo.user_id : ''
      }
    }).then((res) => {
      console.log(res);
      that.showLineList(res);
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },

  onCollect(e){
    console.log(e.currentTarget.dataset.id);
    let that = this;
    let thisId = e.currentTarget.dataset.id;
    let is_coll = e.currentTarget.dataset.coll << 0;
    let userInfo = util.isLogin();
    let title = '收藏成功';
    util.http({
      url: 'api/miniwechat/index/coll',
      method: 'POST',
      data: {
        user_id: userInfo.user_id,
        line_id: thisId,
        type: 1 ^ is_coll
      }
    }).then(res => {
      that.data.allLineList.forEach((item, index) => {
        if (item.line_id == thisId) {
          item.is_coll = 1 ^ is_coll;
        }
      })

      let lArr = that.data.allLineList.filter((item, index) => index % 2 === 0);
      let rArr = that.data.allLineList.filter((item, index) => index % 2 === 1);
      //wxml文件里面需要改变一下样式
      that.setData({
        lLineList: lArr,
        rLineList: rArr
      })
      if (is_coll == 1){
        title = '取消' + title;
      }
      wx.showToast({
        title: title,
        icon: 'none',
        duration: 2000
      })
    }).catch(ex => {

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    //分页请求更多数据
    let options = this.data.option;
    if (options.type == "theme") {
      this.createTheme(options.id)
    } else {
      this.createSpots(options.id)
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
function timeReset(time) {
  let arr = time.split("-")
  arr.splice(0, 1)
  arr = arr.join(".")
  return arr
}